package com.test.travelinformation.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.MenuItem;

import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.test.travelinformation.R;
import com.test.travelinformation.adapter.MyFragAdapter;
import com.test.travelinformation.fragment.Homefragment;
import com.test.travelinformation.fragment.Myfragment;
import com.test.travelinformation.fragment.Releasefragment;
import com.test.travelinformation.utils.SPUtils;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    ViewPager viewPager;
    BottomNavigationView navigation;
    List<Fragment> listFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        isLogin();
    }

    private void initView() {
        viewPager = findViewById(R.id.view_pager);
        navigation = findViewById(R.id.navigation);
        listFragment = new ArrayList<>();
        listFragment.add(new Homefragment());
        listFragment.add(new Releasefragment());
        listFragment.add(new Myfragment());
        viewPager.setAdapter(new MyFragAdapter(getSupportFragmentManager(), this, listFragment));
        navigation.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()) {

                    case R.id.navigation_home:
                        viewPager.setCurrentItem(0);
                        return true;
                    case R.id.navigation_release:
                        viewPager.setCurrentItem(1);
                        return true;
                    case R.id.navigation_my:
                        viewPager.setCurrentItem(2);
                        return true;
                }
                return false;
            }
        });
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                navigation.getMenu().getItem(position).setChecked(true);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
    }


    private void isLogin() {
        String userId = SPUtils.get(this, "userId", "").toString();
        Log.e("BBBBBBBBBBB", userId);
        if ("".equals(userId) || TextUtils.isEmpty(userId)) {
            startActivity(new Intent(this, LoginActivity.class));
            finish();
        }
    }

}